﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.ProcurementsCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler
{
    /// <summary>
    ///修改开票通知单的审批状态处理方法 
    /// </summary>
    public class UdpShenPiStatusCommandHandler : IRequestHandler<UdpShenPiStatusCommand, APIResult<int>>
    {
        private readonly IBaseRepository<InvoiceSettlementEntity> iRepo;

        public UdpShenPiStatusCommandHandler(IBaseRepository<InvoiceSettlementEntity> iRepo)
        {
            this.iRepo = iRepo;
        }

        public Task<APIResult<int>> Handle(UdpShenPiStatusCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var res = iRepo.GetAll().Result.Where(x=>x.InvoiceSettlementEntityId==request.InvoiceSettlementEntityId).FirstOrDefault();
            if(res.ShenPiStatus == "待审核")
            {
                res.ShenPiStatus = "已审核";      
                var r =  iRepo.Update(res).Result;
                if(r>0)
                {
                    result.data =r;
                    result.Code = APIEnum.修改成功;
                    result.Msg = APIEnum.修改成功.ToString();
                    return Task.FromResult(result);
                }
                else
                {
                    result.Code = APIEnum.修改失败;
                    result.Msg = APIEnum.修改失败.ToString();
                    result.data = r;
                }
                
            }
           else if (res.ShenPiStatus == "已审核")
            {
                res.ShenPiStatus = "已完成--通过";
                var r = iRepo.Update(res).Result;
                if (r > 0)
                {
                    result.Code = APIEnum.修改成功;
                    result.Msg = APIEnum.修改成功.ToString();
                    result.data = r;
                }
                else
                {
                    result.Code = APIEnum.修改失败;
                    result.Msg = APIEnum.修改失败.ToString();
                    result.data = r;
                }
            }

           return Task.FromResult(result);




        }



    }
}
